package com.achievement.project.system.proranking.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.achievement.common.utils.security.ShiroUtils;
import com.achievement.project.system.student.domain.Student;
import com.achievement.project.system.student.service.IStudentService;
import com.achievement.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.achievement.framework.aspectj.lang.annotation.Log;
import com.achievement.framework.aspectj.lang.enums.BusinessType;
import com.achievement.project.system.proranking.domain.Proranking;
import com.achievement.project.system.proranking.service.IProrankingService;
import com.achievement.framework.web.controller.BaseController;
import com.achievement.framework.web.domain.AjaxResult;
import com.achievement.common.utils.poi.ExcelUtil;
import com.achievement.framework.web.page.TableDataInfo;

/**
 * 专业排名Controller
 *
 * @author zkai
 * @date 2021-05-10
 */
@Controller
@RequestMapping("/system/proranking")
public class ProrankingController extends BaseController
{
    private String prefix = "system/proranking";

    @Autowired
    private IProrankingService prorankingService;
    @Autowired
    private IStudentService studentService;

    @RequiresPermissions("system:proranking:view")
    @GetMapping()
    public String proranking()
    {
        return prefix + "/proranking";
    }

    /**
     * 查询专业排名列表
     */
    @RequiresPermissions("system:proranking:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Proranking proranking)
    {
        List<Proranking> list =  new ArrayList<>();
        List<Proranking> list1 =  new ArrayList<>();
        List<Proranking> listForTable =  new ArrayList<>();
        List<Proranking> list_last=  new ArrayList<>();
        startPage();
        Long userId = ShiroUtils.getUserId();
        Student student = studentService.selectStudentById(userId);
        Proranking prorankingForTable = new Proranking();
        prorankingForTable.setsId(userId);
        listForTable.add(prorankingForTable);
        String semester = proranking.getSemester();
        //根据 班级id和 课程id 查询出所有该课程的学生的成绩 学分等
        List<Map<String, Object>> courseAvgPoint = prorankingService.getCourseAvgPoint(student.getSemester());
        //遍历获取
        for (Map<String, Object> map:courseAvgPoint
        ) {
            Proranking p = new Proranking();
            p.setsId(Long.valueOf(map.get("student_id").toString()));
            p.setsNo(Long.valueOf(map.get("s_no").toString()));
            p.setSemester(map.get("semester").toString());
            //单个学生  绩点*课程所得之和   课程学分之和
            BigDecimal point = new BigDecimal(map.get("point").toString()) ;
            BigDecimal credit = new BigDecimal(map.get("credit").toString());
            if (point.equals(0)){
                p.setPointAverage("0");
            }else {
                p.setPointAverage(point.divide(credit,2,BigDecimal.ROUND_HALF_UP).toString());
            }
        list.add(p);
        }

        //获取专业平均
        List<Map<String, Object>> courseTypeAvgPoint = prorankingService.getCourseTypeAvgPoint(student.getSemester());
        for (Map<String, Object> map:courseTypeAvgPoint
        ) {
            Proranking p1 = new Proranking();
            p1.setsId(Long.valueOf(map.get("student_id").toString()));
            p1.setsNo(Long.valueOf(map.get("s_no").toString()));
            p1.setSemester(map.get("semester").toString());
            BigDecimal point = new BigDecimal(map.get("point").toString()) ;
            BigDecimal credit = new BigDecimal(map.get("credit").toString());
            if (point.equals(0)){
                p1.setPointAverage("0");
            }else {
                p1.setProfessionalAverage(point.divide(credit,2,BigDecimal.ROUND_HALF_UP).toString());
            }
            list1.add(p1);
        }


        //按平均绩点排序(Integer类型)
         list = list.stream().sorted(Comparator.comparing(Proranking::getPointAverage)).collect(Collectors.toList());
        //按专业平均进行排序
        list1 = list1.stream().sorted(Comparator.comparing(Proranking::getProfessionalAverage)).collect(Collectors.toList());
        Proranking newP = listForTable.get(0);
        for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getsId().equals(newP.getsId())){
                    newP.setSemester(list.get(i).getSemester());
                    newP.setPointRanking(String.valueOf(i+1));
                    newP.setPointAverage(list.get(i).getPointAverage());
                }
        }

        for (int i = 0; i < list1.size(); i++) {
                if (list1.get(i).getsId().equals(newP.getsId())){
                    newP.setSemester(list1.get(i).getSemester());
                    newP.setProfessionalRanking(String.valueOf(i+1));
                    newP.setProfessionalAverage(list1.get(i).getProfessionalAverage());
                }
            }
        list_last.add(newP);
        return getDataTable(list_last);
    }

    /**
     * 导出专业排名列表
     */
    @RequiresPermissions("system:proranking:export")
    @Log(title = "专业排名", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Proranking proranking)
    {
        List<Proranking> list = prorankingService.selectProrankingList(proranking);
        ExcelUtil<Proranking> util = new ExcelUtil<Proranking>(Proranking.class);
        return util.exportExcel(list, "专业排名数据");
    }

    /**
     * 新增专业排名
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存专业排名
     */
    @RequiresPermissions("system:proranking:add")
    @Log(title = "专业排名", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Proranking proranking)
    {
        return toAjax(prorankingService.insertProranking(proranking));
    }

    /**
     * 修改专业排名
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Proranking proranking = prorankingService.selectProrankingById(id);
        mmap.put("proranking", proranking);
        return prefix + "/edit";
    }

    /**
     * 修改保存专业排名
     */
    @RequiresPermissions("system:proranking:edit")
    @Log(title = "专业排名", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Proranking proranking)
    {
        return toAjax(prorankingService.updateProranking(proranking));
    }

    /**
     * 删除专业排名
     */
    @RequiresPermissions("system:proranking:remove")
    @Log(title = "专业排名", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(prorankingService.deleteProrankingByIds(ids));
    }
}
